<?php

//小站选项应用模型News模块
class News_model extends Model {

	function News_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("plugins_model", "pm");
		$this->load->model("comment_model");
		$this->load->model("options_model");
	}
	
	//获取列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("module_news")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach($items as $k => $list){
				$siteid = $this->pm->get_siteid($list["plid"]);
				$site = $this->sm->get($siteid);
				if($site) {
					$items[$k]["domain"] = $site["domain"];
					$items[$k]["sitename"] = $site["name"];
					$items[$k]["logo"] = $site["logo"];
				} else {
					$items[$k]["domain"] = $items[$k]["sitename"] = $items[$k]["logo"] = "";
				}
				
				//用户信息
				$userid = $list["userid"];
				$userinfo = $this->am->get($userid);
				$items[$k]["userinfo"] = $userinfo;
			}
		}
		return $items;
	}
	
	//计算总数
	function countitems()
	{
		return $this->db->count_all_results("module_news");
	}
	
	//添加
	function add($plid)
	{
		$title = strip_tags(p("title"));
		$description = p("description");
		//验证图片
		$pics = get_content_pics($description);
		/*//最多5张图片
		if(count($pics) == 0 || count($pics) > 5) {
			return false;
		}*/
		
		//第一张图片
		if($pics && is_array($pics) && count($pics) > 0) {
			$firstpic = $pics[0];
			$filename = str_replace("/data/upload_ck/", "", $firstpic);
			$ext = get_imgext($firstpic);
			$newfilename = str_replace(".".$ext, "", $filename)."_crop.".$ext;
			//裁剪图片
			$params = array('src_file' => './data/upload_ck/'.$filename, 'dst_file' => './data/upload_ck/'.$newfilename);
			///$this->load->library("imagecrop", $params);
			if(file_exists($params['src_file'])) {
				$this->load->library("imagecrop");
				$this->imagecrop->src_file=$params['src_file'];
				$this->imagecrop->dst_file=$params['dst_file'];
				$this->imagecrop->LoadImage();
				$this->imagecrop->Crop(120,160,1);
				$this->imagecrop->SaveImage();
				$this->imagecrop->destory();
				$firstpic = "/data/upload_ck/".$newfilename;
			} else {
				$firstpic = "";
			}
			
		} else {
			$firstpic = "";
		}
		$data = array(
			"userid" => $this->userid,
			"title" => $title,
			"description" => $description,
			"plid" => $plid,
			"firstpic" => $firstpic,
			"addtime" => time(),
		);
		$result = $this->db->insert("module_news", $data);
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}
	
	//编辑
	function edit($plid)
	{
		$id = p("id");
		$title = strip_tags(p("title"));
		$description = p("description");
		//验证图片
		$pics = get_content_pics($description);
		if($pics && is_array($pics) && count($pics) > 0) {
			$firstpic = $pics[0];
			$filename = str_replace("/data/upload_ck/", "", $firstpic);
			$ext = get_imgext($firstpic);
			$newfilename = str_replace(".".$ext, "", $filename)."_crop.".$ext;
			//裁剪图片
			if(file_exists('./data/upload_ck/'.$filename)) {
				$params = array('src_file' => './data/upload_ck/'.$filename, 'dst_file' => './data/upload_ck/'.$newfilename);
				//$this->load->library("imagecrop", $params);
				if(file_exists($params['src_file'])) {
					$this->load->library("imagecrop");
					$this->imagecrop->src_file=$params['src_file'];
					$this->imagecrop->dst_file=$params['dst_file'];
					$this->imagecrop->LoadImage();
					$this->imagecrop->Crop(120,160,1);
					$this->imagecrop->SaveImage();
					$this->imagecrop->destory();
					$firstpic = "/data/upload_ck/".$newfilename;
				} else {
					$firstpic = "";
				}
				
			}
		} else {
			$firstpic = "";
		}
		$data = array(
			"userid" => $this->userid,
			"title" => $title,
//			"description" => strip_tags($description,'<a><img><br>'),
			"description" => $description,
			"plid" => $plid,
			"firstpic" => $firstpic,
			"addtime" => time(),
		);
		$this->db->where("id", $id);
		$result = $this->db->update("module_news", $data);
		return $result;
	}
	
	//删除
	function delete($dataid)
	{
		$result = $this->db->delete("module_news", array("id"=>$dataid));
		return $result;
	}
	
	//删除选择话题
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//删除评论
//		$this->comment_model->forum_delete($id);
		$this->db->where_in("dataid", $delid);
		$this->db->delete("comment");
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("module_news");
		return $result;
	}
	
	//预览
	function preivew()
	{
		//a(p("title"));
		$viewdata = array();
		$id = 0;
		if(p("title") && p("description")) {
			$viewdata["title"] = p("title");
			$viewdata["description"] = p("description");
			$viewdata["id"] = p("id");
		}
		return $viewdata;
	}
	
	//#获取列表
	function lists($plid, $limit = 5, $offset = 0)
	{
		$lists = array();
		$query = $this->db->from("module_news")->where(array("plid"=>$plid))->order_by("id","desc")->limit($limit, $offset)->get();
		if($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row) {
				//模块ID
				$plid = $row["plid"];
				$r = $this->pm->get($plid);
				$lists[$k]["moduleid"] = $r["moduleid"];
				//评论总数
				$opid = $r["opid"];
				$r = $this->options_model->get($opid);
				$siteid = $r["siteid"];
				$commentcount = $this->comment_model->count_lists($siteid, $plid, $row["id"]);
				$lists[$k]["commentcount"] = $commentcount;
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_lists($plid)
	{
		$query = $this->db->from("module_news")->where(array("plid"=>$plid))->get();
		return $query->num_rows();
	}
	
	//#查看内容
	function show($id)
	{
		$row = array();
		$query = $this->db->get_where("module_news", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			$r = $this->pm->get($row["plid"]);
			if(!$r) show_404();
			$row["moduleid"] = $r["moduleid"];
			//评论总数
			$opid = $r["opid"];
			$r = $this->options_model->get($opid);
			if ($r) {
				$siteid = $r["siteid"];
				$row["siteid"] = $siteid;
				$commentcount = $this->comment_model->count_lists($siteid, $row["plid"], $row["id"]);
				$row["commentcount"] = $commentcount;
				//小站信息
				$row["site"] = $this->sm->get_site($siteid);
			} else {
				$row["site"]["name"] = '';
			}
			//用户信息
			$userid = $row["userid"];
			$userinfo = $this->am->get($userid);
			$row["userinfo"] = $userinfo;
			
		}
		return $row;
	}
	
	//#查看内容
	function get_info($id)
	{
		$row = array();
		$query = $this->db->get_where("module_news", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			$r = $this->pm->get($row["plid"]);
			$row["moduleid"] = $r["moduleid"];
		}
		return $row;
	}
}

/* End of file */